package wowmobile;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

class Communication {

    private DataInputStream in;
    private DataOutputStream out;

    public Communication(Socket channel)
    {
        try
        {
            InputStream is=channel.getInputStream();
            OutputStream os=channel.getOutputStream();
            out=new DataOutputStream(os);
            in=new DataInputStream(is);
        }
        catch(Exception e)
        {
            System.err.println("Error in Communication Constructor"+e);
            return;
        }
    }

    public void sendAction(Action a) throws IOException
    {
        String tmp=a.toString();
        out.writeUTF(tmp);
    }

    public Action getAction() throws IOException, ClassNotFoundException
    {
        Action a;
        String s;
        s= in.readUTF();
        a=Action.fromString(s);
        return a;
    }

    public void sendPlayers(String s) throws IOException
    {
        out.writeUTF(s);
    }

    void sendArea(Area area) throws IOException
    {
        out.writeUTF(area.toString());
    }

    public void sendAnswer(String s) throws IOException
    {
        out.writeUTF(s);
    }

    public String getFightAction() throws IOException
    {
        return in.readUTF();
    }

    public void sendFight(String s) throws IOException
    {
        out.writeUTF(s);
    }

    void sendStartFight(ArrayList<Player> lp, ArrayList<Monster> ml) throws IOException
    {
        String tmp="";
        tmp+=lp.size();
        tmp+=" ";
        tmp+=ml.size();
        /*Player tmpP;
        for(int i=0; i<lp.size(); i++)
        {
            tmpP=lp.get(i);
            tmp+=" "+tmpP.getName()+" "+tmpP.getStats().getImage()+" "+tmpP.getStats().getHp()+" "+tmpP.getStats().getMana();
        }
        Monster tmpM;
        for(int i=0; i<ml.size(); i++)
        {
            tmpM=ml.get(i);
            tmp+=" "+tmpM.getName()+" "+tmpM.getImage();
        }*/
        out.writeUTF(tmp);
    }
    public String getLogin() throws IOException
    {
        return in.readUTF();
    }
    
    public void sendIsLogin(Boolean isLogin) throws IOException{
        String s = isLogin.toString();
        out.writeUTF(s);
    }
}
